package dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import util.HibernateUtil;
import domain.Contact;

public class DAOContact_old {

	public void addContact(Contact c) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.clear();
			// session.createCriteria(arg0, arg1)
			session.persist(c);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	public void delete(int id) {

		Session session = HibernateUtil.getSessionFactory().openSession();

		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Contact theContact = (Contact) session.load(Contact.class, id);
			if (theContact != null) {
				session.delete(theContact);
			}
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}

	}

	public Contact searchById(int id) {

		Contact contact = new Contact();
		return contact;
	}

	/**
	 * returns all stored contacts
	 */
	public List<Contact> search() {

		List<Contact> contacts = new ArrayList<Contact>();

		Session session = HibernateUtil.getSessionFactory().openSession();

		try {
			Query query = session.createQuery("from Contact");
			List Stations = query.list();
			for (Iterator iter = Stations.iterator(); iter.hasNext();) {
				contacts.add((Contact) iter.next());
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}

		return contacts;
	}

}
